<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <SearchForm
      ref="searchForm"
      :show-search="showSearch"
      @query="handleQuery"
    />
    
    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['logistics:shipment:export']"
        >
          导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="handleRefresh"
        >
          刷新
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    
    <!-- 运单列表表格 -->
    <ShipmentTable
      :shipment-list="shipmentList"
      :loading="loading"
      :total="total"
      :query-params="queryParams"
      @selection-change="handleSelectionChange"
      @detail="handleDetail"
      @pagination="handlePagination"
    />
    
    <!-- 轨迹详情抽屉 -->
    <TrackingDrawer
      :visible="trackingVisible"
      :shipment-info="currentShipment"
      @close="handleTrackingClose"
    />
  </div>
</template>

<script>
import SearchForm from './components/SearchForm'
import ShipmentTable from './components/ShipmentTable'
import TrackingDrawer from './components/TrackingDrawer'
import { getShipmentComprehensive } from './api/comprehensive'

export default {
  name: 'ShipmentStatus',
  components: {
    SearchForm,
    ShipmentTable,
    TrackingDrawer
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运单表格数据
      shipmentList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shipmentCode: undefined,
        status: undefined,
        destinationProvince: undefined,
        destinationCity: undefined,
        startDate: undefined,
        endDate: undefined
      },
      // 轨迹抽屉显示状态
      trackingVisible: false,
      // 当前选中的运单
      currentShipment: null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询运单列表 */
    async getList() {
      this.loading = true
      try {
        const response = await getShipmentComprehensive(this.queryParams)
        if (response.code === 200) {
          this.shipmentList = response.data.rows
          this.total = response.data.total
        }
      } catch (error) {
        console.error('查询运单列表失败:', error)
        this.$message.error('查询运单列表失败')
      } finally {
        this.loading = false
      }
    },
    
    /** 搜索按钮操作 */
    handleQuery(params) {
      this.queryParams = {
        ...this.queryParams,
        ...params,
        pageNum: 1
      }
      this.getList()
    },
    
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.searchForm.resetQuery()
    },
    
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    
    /** 详情按钮操作 */
    handleDetail(row) {
      this.currentShipment = row
      this.trackingVisible = true
    },
    
    /** 分页操作 */
    handlePagination(pagination) {
      this.queryParams.pageNum = pagination.pageNum
      this.queryParams.pageSize = pagination.pageSize
      this.getList()
    },
    
    /** 导出按钮操作 */
    handleExport() {
      this.download('logistics/shipment/export', {
        ...this.queryParams
      }, `shipment_status_${new Date().getTime()}.xlsx`)
    },
    
    /** 刷新按钮操作 */
    handleRefresh() {
      this.getList()
      this.$message.success('数据已刷新')
    },
    
    /** 关闭轨迹抽屉 */
    handleTrackingClose() {
      this.trackingVisible = false
      this.currentShipment = null
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
  
  .mb8 {
    margin-bottom: 8px;
  }
  
  ::v-deep .el-card {
    border-radius: 4px;
    
    .el-card__header {
      padding: 18px 20px;
      border-bottom: 1px solid #ebeef5;
      
      .card-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        
        .card-title {
          font-size: 16px;
          font-weight: 500;
          color: #303133;
        }
      }
    }
    
    .el-card__body {
      padding: 20px;
    }
  }
  
  ::v-deep .el-table {
    .el-table__header-wrapper {
      th {
        background-color: #fafafa;
        color: #606266;
        font-weight: 500;
      }
    }
    
    .el-table__row {
      &:hover {
        background-color: #f5f7fa;
      }
    }
  }
  
  ::v-deep .el-pagination {
    margin-top: 20px;
    text-align: right;
  }
}

// 响应式布局
@media (max-width: 768px) {
  .app-container {
    padding: 10px;
    
    ::v-deep .el-form--inline {
      .el-form-item {
        display: block;
        margin-right: 0;
        margin-bottom: 15px;
        
        .el-form-item__content {
          margin-left: 0 !important;
        }
      }
    }
    
    ::v-deep .el-table {
      font-size: 12px;
      
      .el-table__cell {
        padding: 6px 0;
      }
    }
  }
}
</style>